package com.teyao.teyao_back.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.teyao.teyao_back.config.myPage;
import com.teyao.teyao_back.mapper.ClientDeliverMapper;
import com.teyao.teyao_back.entity.ClientDeliverWrapper;
import com.teyao.teyao_back.entity.ResponseMessage;
import com.teyao.teyao_back.service.ClientDeliverService;
import org.springframework.stereotype.Service;

@Service
public class ClientDeliverImpl extends ServiceImpl<ClientDeliverMapper, ClientDeliverWrapper> implements ClientDeliverService {
    @Override
    public ResponseMessage selectAll() {
        ResponseMessage responseMessage = new ResponseMessage();
        QueryWrapper<ClientDeliverWrapper> queryWrapper = new QueryWrapper<>();
        queryWrapper.ne("status", "退货中");
        responseMessage.setData(baseMapper.selectList(queryWrapper));
        return responseMessage;
    }

    @Override
    public ResponseMessage selectReturn() {
        ResponseMessage responseMessage = new ResponseMessage();
        QueryWrapper<ClientDeliverWrapper> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status", "退货中");
        responseMessage.setData(baseMapper.selectList(queryWrapper));
        return responseMessage;
    }

    @Override
    public ResponseMessage selectNotStart() {
        ResponseMessage responseMessage = new ResponseMessage();
        QueryWrapper<ClientDeliverWrapper> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status", "未配货");
        responseMessage.setData(baseMapper.selectList(queryWrapper));
        return responseMessage;
    }

    @Override
    public myPage MyQuery(Integer currentPage, Integer size, ClientDeliverWrapper clientDeliverWrapper) {
        QueryWrapper<ClientDeliverWrapper> queryWrapper = new QueryWrapper<>();
        if (clientDeliverWrapper.getClient() != null && !clientDeliverWrapper.getClient().isBlank()) {
            queryWrapper.like("client", clientDeliverWrapper.getClient().trim());
        }
        if (clientDeliverWrapper.getOrderId() != null && !clientDeliverWrapper.getOrderId().isBlank()) {
            queryWrapper.like("order_id", clientDeliverWrapper.getOrderId());
        }
        if (clientDeliverWrapper.getStatus() != null && !clientDeliverWrapper.getStatus().isBlank()) {
            queryWrapper.eq("status", clientDeliverWrapper.getStatus());
        }
        myPage myPage = new myPage();
        myPage.setCurrentPage(currentPage);
        Page<ClientDeliverWrapper> page = new Page(myPage.getCurrentPage(), size);
        IPage result = baseMapper.selectPage(page, queryWrapper);
        myPage.setTotal((int) result.getTotal());
        myPage.setData(result.getRecords());
        return myPage;
    }

    @Override
    public myPage MyQueryClient(Integer currentPage, Integer size, Integer userId, ClientDeliverWrapper clientDeliverWrapper) {
        QueryWrapper<ClientDeliverWrapper> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        if (clientDeliverWrapper.getClient() != null && !clientDeliverWrapper.getClient().isBlank()) {
            queryWrapper.like("client", clientDeliverWrapper.getClient().trim());
        }
        if (clientDeliverWrapper.getOrderId() != null && !clientDeliverWrapper.getOrderId().isBlank()) {
            queryWrapper.like("order_id", clientDeliverWrapper.getOrderId());
        }
        if (clientDeliverWrapper.getStatus() != null && !clientDeliverWrapper.getStatus().isBlank()) {
            queryWrapper.eq("status", clientDeliverWrapper.getStatus());
        }
        myPage myPage = new myPage();
        myPage.setCurrentPage(currentPage);
        Page<ClientDeliverWrapper> page = new Page(myPage.getCurrentPage(), size);
        IPage result = baseMapper.selectPage(page, queryWrapper);
        myPage.setTotal((int) result.getTotal());
        myPage.setData(result.getRecords());
        return myPage;
    }


    //王
    @Override
    public myPage getByExample(ClientDeliverWrapper clientDeliverWrapper, Integer pageSize, long current, String status) {

        QueryWrapper<ClientDeliverWrapper> queryWrapper = new QueryWrapper<>();
        if (clientDeliverWrapper.getClient() != null && !clientDeliverWrapper.getClient().isBlank()) {
            queryWrapper.like("client", clientDeliverWrapper.getClient().trim());
        }
        if (clientDeliverWrapper.getAddress() != null && !clientDeliverWrapper.getAddress().isBlank()) {
            queryWrapper.like("address", clientDeliverWrapper.getAddress().trim());
        }
        if (clientDeliverWrapper.getOrderId() != null && !clientDeliverWrapper.getOrderId().isBlank()) {
            queryWrapper.like("order_id", clientDeliverWrapper.getOrderId().trim());
        }
        QueryWrapper<ClientDeliverWrapper> queryWrapper2 = new QueryWrapper<>();
        if (status.equals("已退货")) {
            System.out.println("TTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTT");
            queryWrapper.eq("status", status).or().eq("status", "已退库");
            //        或者
            if (clientDeliverWrapper.getClient() != null && !clientDeliverWrapper.getClient().isBlank()) {
                queryWrapper.like("client", clientDeliverWrapper.getClient().trim());
            }
            if (clientDeliverWrapper.getAddress() != null && !clientDeliverWrapper.getAddress().isBlank()) {
                queryWrapper.like("address", clientDeliverWrapper.getAddress().trim());
            }
            if (clientDeliverWrapper.getOrderId() != null && !clientDeliverWrapper.getOrderId().isBlank()) {
                queryWrapper.like("order_id", clientDeliverWrapper.getOrderId().trim());
            }
        } else {
            queryWrapper.eq("status", status);
        }


        IPage<ClientDeliverWrapper> page = new Page<>();
        page.setSize(pageSize);
        page.setCurrent(current);
        System.out.println(page.getCurrent() + "-" + page.getSize());
        IPage<ClientDeliverWrapper> orderPage = baseMapper.selectPage(page, queryWrapper);
        myPage result = new myPage();
        result.setSuccess(true);
        result.setData(orderPage.getRecords());
        result.setTotal((int) orderPage.getTotal());
        return result;
    }
}
